package com.dwy2002.product;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dwy2002.common.Result;
import com.dwy2002.common.annotation.RequiresPermission;
import com.dwy2002.enums.Logical;
import com.dwy2002.pojo.dto.product.ProductPageDTO;
import com.dwy2002.pojo.dto.product.ProductsDTO;
import com.dwy2002.pojo.po.product.Products;
import com.dwy2002.pojo.vo.product.ProductDetailVO;
import com.dwy2002.product.service.IProductsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/02/18
 */
@RestController
@RequestMapping("/shopping/product")
@RequiredArgsConstructor
public class ProductController {
    private final IProductsService service;

    /**
     * 添加商品
     *
     * @param product 接收前端的商品数据
     * @author 杜伟毅
     * @since 2025/2/19 0019 0:01
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @PostMapping
    public Result<Void> saveProduct(@RequestBody ProductsDTO product) {
        return service.saveProduct(product);
    }

    /**
     * 修改商品
     *
     * @param product 接收前端的商品数据
     * @author 杜伟毅
     * @since 2025/2/19 0019 16:33
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @PutMapping
    public Result<Void> updateProduct(@RequestBody ProductsDTO product) {
        return service.updateProduct(product);
    }

    /**
     * 删除商品（支持单个删除和批量删除）
     *
     * @param ids 商品id集合
     * @author 杜伟毅
     * @since 2025/2/19 0019 23:55
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @DeleteMapping
    public Result<Void> deleteListProduct(@RequestBody List<String> ids) {
        return service.deleteListProduct(ids);
    }

    /**
     * 分页查询
     *
     * @param productPage 商品分页数据
     * @author 杜伟毅
     * @since 2025/2/26 0026 16:41
     */
    @PostMapping("/page")
    public Result<Page<Products>> getProductPage(@RequestBody ProductPageDTO productPage) {
        return service.getProductPage(productPage);
    }

    /**
     * 获取商品详情
     *
     * @param id 商品id
     * @author 杜伟毅
     * @since 2025/2/27 0027 11:05
     */
    @GetMapping("/{id}")
    public Result<ProductDetailVO> getProductDetail(@PathVariable String id) throws ExecutionException, InterruptedException {
        return service.getProductDetail(id);
    }

    /**
     * 下架商品
     *
     * @param id 商品id
     * @author 杜伟毅
     * @since 2025/3/2 0002 14:56
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @PutMapping("/takeOff/{id}")
    public Result<Void> updateProduct(@PathVariable String id) {
        return service.takeOffProduct(id);
    }

    /**
     * 上架商品
     *
     * @param id 商品id
     * @author 杜伟毅
     * @since 2025/3/2 0002 15:09
     */
    @RequiresPermission(value = {"A8"}, logical = Logical.OR)
    @PutMapping("/list/{id}")
    public Result<Void> listProduct(@PathVariable String id) {
        return service.listProduct(id);
    }

    /**
     * 获取商品单位
     *
     * @author 杜伟毅
     * @since 2025/5/9 0009 17:34
     */
    @GetMapping
    public Result<List<String>> getProductUnit() {
        return service.getProductUnit();
    }
}
